package com.zjc615.api;

import com.zjc615.api.param.MallUserLoginParam;
import com.zjc615.api.param.MallUserRegisterParam;
import com.zjc615.api.param.MallUserUpdateParam;
import com.zjc615.api.vo.NewBeeMallUserVO;
import com.zjc615.common.Constants;
import com.zjc615.common.ServiceResultEnum;
import com.zjc615.config.annotation.TokenToMallUser;
import com.zjc615.entity.MallUser;
import com.zjc615.service.NewBeeMallUserService;
import com.zjc615.util.NumberUtil;
import com.zjc615.util.Result;
import com.zjc615.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(value = "v1",tags = "新峰商城用户操作接口")
@RequestMapping("/api/v1")
public class NewBeeMallPersonalAPI {

    @Resource
    private NewBeeMallUserService newBeeMallUserService;

    private static final Logger logger = LoggerFactory.getLogger(NewBeeMallPersonalAPI.class);

    @PostMapping("/user/login")
    @ApiOperation(value = "登录接口", notes = "返回token")
    public Result<String> login(@RequestBody MallUserLoginParam mallUserLoginParam){
        String loginResult = newBeeMallUserService.login(mallUserLoginParam.getLoginName(), mallUserLoginParam.getPasswordMd5());
        // 打印登录的用户名和登录结果到控制台
        logger.info("login api,loginName={},loginResult={}",mallUserLoginParam.getLoginName(),loginResult);

        //登录成功
        if (StringUtils.hasText(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH) {
            Result result = ResultGenerator.genSuccessResult();
            result.setData(loginResult);
            return result;
        }
        //登录失败
        return ResultGenerator.genFailResult(loginResult);
    }

    @GetMapping(value = "/test1")
    @ApiOperation(value = "测试接口", notes="方法中含有@TokenToMallUser注解")
    public Result<String> test1(@TokenToMallUser MallUser user){
        Result result = null;
        if (user == null)
        {
            result = ResultGenerator.genErrorResult(416,"未登录！");
            return result;
        }
        else
        {
            result = ResultGenerator.genSuccessResult("登录验证通过");
        }
        return result;
    }

    @GetMapping(value = "/test2")
    @ApiOperation(value = "测试接口", notes="方法中无@TokenToMallUser注解")
    public Result<String> test2(){
        Result result = null;
        result = ResultGenerator.genSuccessResult("此接口无需登录验证，请求成功");
        return result;
    }

    /**
     * 获取用户信息接口
     *
     * 前端需要向后端发送token，通过解析token获取到用户信息MallUser，提取MallUser的昵称、登录名，以及个性签名属性到一个VO，然后把VO传到前端
     * @param loginMallUser
     * @return
     */
    @GetMapping("user/info")
    @ApiOperation(value="获取用户信息", notes="")
    public Result<String> getUserDetail(@TokenToMallUser MallUser loginMallUser){
        // 实例化一个具有昵称、登录名，以及个性签名属性的对象
        NewBeeMallUserVO mallUserVO = new NewBeeMallUserVO();
        // 设置昵称
        mallUserVO.setNickName(loginMallUser.getNickName());
        // 设置登录名
        mallUserVO.setLoginName(loginMallUser.getLoginName());
        // 设置个性签名
        mallUserVO.setIntroduceSign(loginMallUser.getIntroduceSign());
        return ResultGenerator.genSuccessResult(mallUserVO);
    }

    /**
     * 修改用户信息
     *
     * @param mallUserUpdateParam
     * @param loginMallUser 只需要token就可以了，其他来自 MallUser 多余的参数不用写
     * @return
     */
    @PutMapping("user/info")
    @ApiOperation(value="修改用户信息", notes="")
    public Result updateInfo(@RequestBody @ApiParam("用户信息") MallUserUpdateParam mallUserUpdateParam,@TokenToMallUser MallUser loginMallUser){
        Boolean flag = newBeeMallUserService.updateUserInfo(mallUserUpdateParam, loginMallUser.getUserId());
        if (flag) {
            //说明已经修改好了用户信息
            Result result = ResultGenerator.genSuccessResult();
            return result;
        }else{
            //说明修改用户信息失败
            Result result = ResultGenerator.genFailResult("修改失败");
            return result;
        }
    }

    /**
     * 登出用户
     *
     * @param loginMallUser 只需要传token就可以了
     * @return
     */
    @PostMapping("/user/logout")
    @ApiOperation(value = "登出接口", notes = "清除token")
    public Result<String> logout(@TokenToMallUser MallUser loginMallUser) {
        Boolean logoutResult = newBeeMallUserService.logout(loginMallUser.getUserId());

        logger.info("logout api,loginMallUser={}", loginMallUser.getUserId());

        //登出成功
        if (logoutResult) {
            return ResultGenerator.genSuccessResult();
        }
        //登出失败
        return ResultGenerator.genFailResult("logout error");
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "用户注册", notes = "")
    public Result register(@RequestBody @Valid MallUserRegisterParam mallUserRegisterParam) {
        if (!NumberUtil.isPhone(mallUserRegisterParam.getLoginName())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        String registerResult = newBeeMallUserService.register(mallUserRegisterParam.getLoginName(), mallUserRegisterParam.getPassword());

        logger.info("register api,loginName={},loginResult={}", mallUserRegisterParam.getLoginName(), registerResult);

        //注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //注册失败
        return ResultGenerator.genFailResult(registerResult);
    }
}
